import { defineConfig } from "vitepress";
import { SitemapStream } from "sitemap";
import { createWriteStream } from "node:fs";
import { resolve } from "node:path";

// https://vitepress.dev/reference/site-config
export default defineConfig({
  srcDir: "lawyerch.cn",
  title: "陈恒律师-个人博客",
  description:
    "专注民商事诉讼与企业法律顾问服务，从业5年以上，为您提供专业、高效的法律解决方案",
  lang: "zh-CN",

  head: [
    ["link", { rel: "icon", href: "/logo.png" }],
    // 搜索引擎验证（放在最前面，确保优先加载）
    ["meta", { name: "baidu-site-verification", content: "codeva-3N7KHlEFLg" }],
    [
      "meta",
      {
        name: "360-site-verification",
        content: "26f9250169a926b6fb9aff1b87690786",
      },
    ],
    ["meta", { name: "sogou_site_verification", content: "sXJWKTkuYY" }],
    [
      "meta",
      {
        name: "google-site-verification",
        content: "9WUmxarkFXrI6jFoenxjiqdrmzH65nk_DsK0S3dsQPU",
      },
    ],
    // SEO 优化
    [
      "meta",
      {
        name: "keywords",
        content:
          "陈恒律师,广州律师,行政复议律师,行政诉讼律师,民商事律师,企业法律顾问,合同纠纷律师,债权债务律师,广东岭南律师事务所",
      },
    ],
    ["meta", { name: "author", content: "陈恒律师" }],
    ["meta", { property: "og:type", content: "website" }],
    ["meta", { property: "og:title", content: "陈恒律师-专业法律服务" }],
    [
      "meta",
      {
        property: "og:description",
        content:
          "专注行政复议、行政诉讼、民商事纠纷，从业5年以上，为您提供专业、高效的法律解决方案",
      },
    ],
    ["meta", { property: "og:url", content: "https://lawyerch.cn" }],
    // AI 搜索引擎优化
    [
      "meta",
      {
        name: "robots",
        content:
          "index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1",
      },
    ],
    ["meta", { name: "googlebot", content: "index, follow" }],
    ["meta", { name: "bingbot", content: "index, follow" }],
    ["meta", { name: "ai-content-declaration", content: "human-authored" }],
    // AI Plugin 声明
    ["link", { rel: "ai-plugin", href: "/.well-known/ai-plugin.json" }],
    [
      "link",
      { rel: "openapi", href: "/openapi.json", type: "application/json" },
    ],
    // 百度统计
    [
      "script",
      {},
      `
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?a3db8965384ba7b46c348738f828cebf";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
    `,
    ],
    // Google Analytics (GA4)
    [
      "script",
      {
        async: "",
        src: "https://www.googletagmanager.com/gtag/js?id=G-SP25XBVCQ0",
      },
    ],
    [
      "script",
      {},
      `
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'G-SP25XBVCQ0');
    `,
    ],
  ],

  // 禁用 VitePress 自动 sitemap，使用自定义生成
  // sitemap: {
  //   hostname: 'https://lawyerch.cn'
  // },

  // 构建钩子：生成完整的 sitemap
  buildEnd: async ({ outDir }: { outDir: string }) => {
    const { Transform } = await import("node:stream");

    // 创建转换流来添加 XSL 样式引用
    const addStylesheet = new Transform({
      transform(chunk, _encoding, callback) {
        let data = chunk.toString();
        // 在第一个 XML 声明后添加样式引用
        if (
          data.includes("<?xml version") &&
          !data.includes("xml-stylesheet")
        ) {
          data = data.replace(
            '<?xml version="1.0" encoding="UTF-8"?>',
            '<?xml version="1.0" encoding="UTF-8"?>\n<?xml-stylesheet type="text/xsl" href="/sitemap.xsl"?>'
          );
        }
        callback(null, data);
      },
    });

    const sitemap = new SitemapStream({ hostname: "https://lawyerch.cn" });
    const writeStream = createWriteStream(resolve(outDir, "sitemap.xml"));

    sitemap.pipe(addStylesheet).pipe(writeStream);

    // 添加页面（包含完整的 SEO 信息）
    const pages = [
      { url: "/", changefreq: "weekly", priority: 1.0 },
      { url: "/pages/services", changefreq: "monthly", priority: 0.9 },
      { url: "/pages/legal-products", changefreq: "monthly", priority: 0.9 },
      { url: "/pages/tech-products", changefreq: "monthly", priority: 0.9 },
      { url: "/pages/about", changefreq: "monthly", priority: 0.8 },
      { url: "/pages/contact", changefreq: "monthly", priority: 0.8 },
      { url: "/articles", changefreq: "weekly", priority: 0.9 },
      { url: "/cases", changefreq: "weekly", priority: 0.9 },
      { url: "/news", changefreq: "daily", priority: 0.9 },
      { url: "/ai-news", changefreq: "daily", priority: 0.9 },
      {
        url: "/cases/administrative-review-case",
        changefreq: "monthly",
        priority: 0.7,
      },
      {
        url: "/cases/administrative-litigation-case",
        changefreq: "monthly",
        priority: 0.7,
      },
      {
        url: "/cases/civil-dispute-case",
        changefreq: "monthly",
        priority: 0.7,
      },
    ];

    pages.forEach((page) => sitemap.write(page));
    sitemap.end();

    return new Promise<void>((resolve) => {
      writeStream.on("finish", () => {
        console.log("✅ Sitemap 生成完成（包含 SEO 信息）");
        resolve();
      });
    });
  },
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    nav: [
      { text: "首页", link: "/" },
      // { text: '法治新闻', link: '/news' },
      // { text: 'AI资讯', link: '/ai-news' },
      { text: "专业文章", link: "/articles" },
      { text: "服务领域", link: "/pages/services" },
      { text: "法律产品", link: "/pages/legal-products" },
      { text: "技术产品", link: "/pages/tech-products" },
      { text: "案例经验", link: "/cases" },
      { text: "关于我", link: "/pages/about" },
      { text: "联系方式", link: "/pages/contact" },
    ],

    sidebar: {
      "/articles/": [
        {
          text: "返回文章列表",
          link: "/articles",
        },
        {
          text: "法律专栏",
          items: [
            { text: "《民法典》合同编重点解读", link: "/articles/civil-law" },
            { text: "股东权益保护的法律途径", link: "/articles/corporate-law" },
            { text: "合同审查的要点与技巧", link: "/articles/contract-law" },
            { text: "企业劳动用工风险防控", link: "/articles/labor-law" },
          ],
        },
      ],
      "/cases/": [
        {
          text: "返回案例列表",
          link: "/cases",
        },
        {
          text: "案例分享",
          items: [
            {
              text: "合同纠纷案例详析",
              link: "/cases/administrative-review-case",
            },
            {
              text: "公司股权案例分析",
              link: "/cases/administrative-litigation-case",
            },
            { text: "债权债务案例解读", link: "/cases/civil-dispute-case" },
          ],
        },
      ],
    },

    // socialLinks: [
    //   {
    //     icon: {
    //       svg: '<svg t="1762869203440" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4686" width="256" height="256"><path d="M669.3 369.4c9.8 0 19.6 0 29.4 1.6C671 245.2 536.9 152 383.2 152 211.6 152 71 269.7 71 416.8c0 85 45.8 156.9 124.2 210.9l-31.1 93.2L273.6 667c39.2 8.2 70.3 16.3 109.5 16.3 9.8 0 19.6 0 31.1-1.6-6.5-21.3-9.8-42.5-9.8-65.4 0.1-135.7 116.2-246.9 264.9-246.9z m-168.4-85c24.5 0 39.2 16.3 39.2 39.2 0 22.9-16.3 39.2-39.2 39.2-24.5 0-47.4-16.4-47.4-39.2 0-24.5 24.6-39.2 47.4-39.2z m-216.3 73.1c-24.7 0-47.8-16.2-47.8-38.8 0-24.3 24.7-38.8 47.8-38.8s39.5 16.2 39.5 38.8c0.1 22.7-16.4 38.8-39.5 38.8z" fill="#24DB5A" p-id="4687"></path><path d="M953.8 613c0-125.9-124.2-227.2-264.8-227.2-148.8 0-266.5 103-266.5 227.2 0 125.9 117.7 227.2 266.5 227.2 31.1 0 62.1-8.2 93.2-16.3l85 47.4-22.9-78.5c62.1-47.4 109.5-109.5 109.5-179.8z m-351.5-39.2c-14.7 0-31.1-14.7-31.1-31.1 0-14.7 16.3-31.1 31.1-31.1 22.9 0 39.2 16.3 39.2 31.1 0 16.4-14.7 31.1-39.2 31.1z m178-7.6c-14.8 0-31.3-14.6-31.3-30.7 0-14.6 16.5-30.7 31.3-30.7 23.1 0 39.5 16.2 39.5 30.7 0 16.2-16.4 30.7-39.5 30.7z" fill="#24DB5A" p-id="4688"></path></svg>'
    //     },
    //     link: '#',
    //     ariaLabel: '微信'
    //   }
    // ],

    footer: {
      message: "专业 · 专注 · 值得信赖",
      copyright:
        'Copyright © 2025 陈恒律师 | <a href="https://beian.miit.gov.cn/" target="_blank">	粤ICP备2024267441号-1</a>',
    },

    // 搜索功能
    search: {
      provider: "local",
      options: {
        translations: {
          button: {
            buttonText: "搜索文档",
            buttonAriaLabel: "搜索文档",
          },
          modal: {
            noResultsText: "无法找到相关结果",
            resetButtonTitle: "清除查询条件",
            footer: {
              selectText: "选择",
              navigateText: "切换",
            },
          },
        },
      },
    },

    // 文档页脚
    docFooter: {
      prev: "上一篇",
      next: "下一篇",
    },

    // 大纲标题
    outline: {
      label: "页面导航",
      level: [2, 3],
    },

    // 最后更新时间
    lastUpdated: {
      text: "最后更新于",
      formatOptions: {
        dateStyle: "short",
        timeStyle: "short",
      },
    },
  },
});
